<template>
    <div class="login-wrap">
        <div class="ms-login">
            <div class="ms-title">学生图书管理系统</div>
            <el-form :model="param" :rules="rules" ref="login" label-width="0px" class="ms-content">
                <el-form-item prop="username">
                    <el-input v-model="param.username" placeholder="username">
                        <el-button slot="prepend" icon="el-icon-lx-people"></el-button>
                    </el-input>
                </el-form-item>
                <el-form-item prop="password">
                    <el-input type="password" placeholder="password" v-model="param.password" @keyup.enter.native="submitForm()">
                        <el-button slot="prepend" icon="el-icon-lx-lock"></el-button>
                    </el-input>
                </el-form-item>
                <div class="login-btn">
                    <el-button class="mybutton1" @click="submitForm()">登录</el-button>
                </div>
            </el-form>
        </div>
    </div>
</template>

<script>
import axios from 'axios';
import session from '../../utils/session';
export default {
    data: function () {
        return {
            param: {
                username: 'admin',
                password: '123456'
            },
            rules: {
                username: [
                    { required: true, message: '请输入用户名', trigger: 'blur' },
                    { min: 1, max: 15, message: '用户名在 1 到 15 个字符', trigger: 'blur' }
                ],
                password: [
                    { required: true, message: '请输入密码', trigger: 'blur' },
                    { min: 6, max: 16, message: '密码长度在 6 到 16 个字符', trigger: 'blur' }
                ]
            }
        };
    },
    methods: {
        submitForm() {
            this.$refs.login.validate((valid) => {
                if (valid) {
                    axios({
                        method: 'post',
                        url: 'http://172.22.10.90:3000/users',
                        params: {
                            userName: this.param.username,
                            userPwd: this.param.password
                        }
                    })
                        .then((res) => {
                            let user_length = 0
                            user_length = res.data.data.length 
                            
                            if (user_length === 0) {
                                this.$message.error('用户或密码错误!')
                                return
                            }
                            this.$message.success('登录成功');
                            session.setStorage('user_info', res.data.data);
                            
                             localStorage.setItem('ms_username', this.param.username);
                            this.$router.push('/home');
                        })
                        .catch((err) => {
                            this.$message.error('请求失败');
                        });
                } else {
                    this.$message.error('请输入账号和密码');
                    return false;
                }
            });
        }
    }
};
</script>

<style scoped>
.login-wrap {
    position: relative;
    width: 100%;
    height: 100%;
    background-image: url(../../assets/img/bg1.jpg);
    background-size: 100%;
}
.mybutton1 {
    border: 0;
    color: #fff;
    border-radius: 15px;
    background-color: rgb(108, 174, 194);
}
.mybutton1:hover {
    border: 0;
    color: #fff;
    border-radius: 15px;
    background-color: rgb(3, 58, 161);
}
.ms-title {
    width: 100%;
    line-height: 60px;
    text-align: center;
    font-size: 25px;
    color: black;
    letter-spacing: 0.5em;
    font-weight: bold;
    border-bottom: 1px solid #ddd;
    background-color: coral;
}
.ms-login {
    position: absolute;
    left: 50%;
    top: 50%;
    width: 350px;
    margin: -190px 0 0 -175px;
    border-radius: 35px;
    background: rgb(255, 255, 255);
    overflow: hidden;
}
.ms-content {
    padding: 40px 30px;
}
.login-btn {
    text-align: center;
    font-family: 'Times New Roman', Times, serif;
}
.login-btn button {
    width: 100%;
    height: 36px;
    margin-bottom: 10px;
    font-size: 18px;
    letter-spacing: 1em;
    font-weight: bold;
    border: none;
}
.login-tips {
    font-size: 14px;
    line-height: 40px;
    color: white;
}
.el-input {
    font-size: 18px;
    margin-top: 10px;
}
</style>
